package com.example.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot01.entity.TeacherClass;
import com.example.springboot01.resp.Response;
import com.example.springboot01.service.TeacherClassService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/teacherClass")
public class TeacherClassController {
    @Resource
    private TeacherClassService teacherClassService;

    @PostMapping
    public Response add(@RequestBody TeacherClass teacherClass){


        final List<Integer> classIds = teacherClass.getClassIds();
        if (classIds == null || classIds.isEmpty()) {
            return Response.ok(null);
        }

        teacherClassService.remove(new LambdaQueryWrapper<TeacherClass>()
                .eq(TeacherClass::getTeacherId, teacherClass.getTeacherId())
        );

        List<TeacherClass> tcList = new ArrayList<>();
        for (Integer c : classIds) {
            TeacherClass tc = new TeacherClass();
            tc.setTeacherId(teacherClass.getTeacherId());
            tc.setClassId(c);
            tcList.add(tc);
        }
        teacherClassService.saveBatch(tcList);
        return Response.ok(null);
    }


    @GetMapping
    public Response list(){
        return Response.ok(teacherClassService.list());
    }


    @GetMapping("/{teacherId}")
    public Response list(@PathVariable Integer teacherId){
        return Response.ok(teacherClassService.list(new LambdaQueryWrapper<TeacherClass>()
                .eq(TeacherClass::getTeacherId, teacherId)
        ));
    }
}
